const StateManager = require("../template")

function testStateManagerStateOperations() {
  const stateManager = new StateManager({ 
    user: { name: 'test', age: 25 },
    settings: { theme: 'dark', lang: 'en' },
    data: [1, 2, 3]
  })

  console.log(
    "1. 嵌套状态获取测试:",
    (() => {
      const userName = stateManager.getState('user.name')
      const userAge = stateManager.getState('user.age')
      const theme = stateManager.getState('settings.theme')
      return userName === 'test' && userAge === 25 && theme === 'dark'
    })()
  )

  console.log(
    "2. 嵌套状态设置测试:",
    (() => {
      stateManager.setState('user.name', 'updated')
      stateManager.setState('settings.theme', 'light')
      const userName = stateManager.getState('user.name')
      const theme = stateManager.getState('settings.theme')
      return userName === 'updated' && theme === 'light'
    })()
  )

  console.log(
    "3. 数组状态操作测试:",
    (() => {
      const data = stateManager.getState('data')
      stateManager.setState('data', [...data, 4, 5])
      const newData = stateManager.getState('data')
      return newData.length === 5 && newData[3] === 4 && newData[4] === 5
    })()
  )

  console.log(
    "4. 复杂状态更新测试:",
    (() => {
      stateManager.updateState('user', (prev) => ({
        ...prev,
        age: prev.age + 1
      }))
      const userAge = stateManager.getState('user.age')
      return userAge === 26
    })()
  )

  console.log(
    "5. 多路径订阅测试:",
    (() => {
      let notifyCount = 0
      stateManager.subscribe('user', () => notifyCount++)
      stateManager.subscribe('settings', () => notifyCount++)
      stateManager.subscribe('data', () => notifyCount++)
      
      stateManager.setState('user.name', 'multi')
      stateManager.setState('settings.lang', 'zh')
      stateManager.setState('data', [1, 2, 3, 4])
      
      return notifyCount === 3
    })()
  )

  console.log(
    "6. 状态快照测试:",
    (() => {
      const snapshot = stateManager.getSnapshot()
      stateManager.setState('user.name', 'snapshot')
      stateManager.restoreSnapshot(snapshot)
      const userName = stateManager.getState('user.name')
      return userName === 'multi' // 恢复到快照前的状态
    })()
  )

  console.log(
    "7. 状态路径获取测试:",
    (() => {
      const paths = stateManager.getStatePaths()
      return paths.includes('user') &&
             paths.includes('settings') &&
             paths.includes('data') &&
             paths.includes('user.name') &&
             paths.includes('user.age')
    })()
  )

  console.log(
    "8. 中间件测试:",
    (() => {
      let middlewareCalled = false
      const middleware = (state, action) => {
        middlewareCalled = true
        return state
      }
      
      stateManager.addMiddleware(middleware)
      stateManager.setState('user.name', 'middleware')
      
      return middlewareCalled === true
    })()
  )

  console.log(
    "9. 中间件移除测试:",
    (() => {
      let middlewareCalled = false
      const middleware = (state, action) => {
        middlewareCalled = true
        return state
      }
      
      stateManager.addMiddleware(middleware)
      stateManager.removeMiddleware(middleware)
      stateManager.setState('user.name', 'no-middleware')
      
      return middlewareCalled === false
    })()
  )

  console.log(
    "10. 状态变化通知测试:",
    (() => {
      let changeCount = 0
      const callback = (newState, oldState) => {
        changeCount++
      }
      
      stateManager.subscribe('user', callback)
      stateManager.setState('user.name', 'notify1')
      stateManager.setState('user.age', 30)
      stateManager.setState('user', { name: 'notify2', age: 31 })
      
      return changeCount === 3
    })()
  )

  console.log(
    "11. this上下文状态操作测试:",
    (() => {
      const stateOpObj = {
        multiplier: 2,
        processState: function(value) {
          const sm = new StateManager({ value: value })
          sm.setState('value', value * this.multiplier)
          return sm.getState('value')
        }
      }
      return stateOpObj.processState(5) === 10
    })()
  )

  console.log(
    "12. 复杂对象this绑定状态操作测试:",
    (() => {
      const stateOpConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(value) {
          const sm = new StateManager({ value: value })
          return sm.getState('value') >= this.config.threshold
        }
      }
      return stateOpConfigObj.processWithConfig(5) === true
    })()
  )
}

testStateManagerStateOperations()
